var app = getApp();
Page(Object.assign({},{
    data:{
        inited : false,
        helplist : [],
		bgmusic : false,
		closebg : false,
		good : {},
        isneedauth : false,
		siteroot : app.siteInfo.siteroot.replace('app/index.php', ''),uniacid: app.siteInfo.uniacid,
    },
	onShow: function () {
		var self = this;
        if( self.data.good.music && !self.data.closebg ) {
            this.replaybg(1);
        }
	},
    onUnload(){
        if( this.data.bgmusic ){
            this.replaybg(2);
        }
    },
    onHide(){
        if( this.data.bgmusic ){
            this.replaybg(2);
        }
    },
    onLoad:function(options){
        var self = this;

        if( options.scene ){
            var list = options.scene.split('_');
            options.id = list[0] ? list[0] : 0;
            options.zfid = list[1] ? list[1] : 0;
        }
        
        var id = options.id ? options.id : (options.scene ? options.scene : '');
        var zfid = options.zfid ? options.zfid : '';
        
        self.setData({
            options : options,
        })
        
        self.data.timer = null;
        clearInterval(self.data.timer);
        
        var loc = wx.getStorageSync('nlloc');
        loc = loc ? loc: {};
        app.com.auth(function(user){
            self.getData(id,zfid,loc.latitude,loc.longitude,true,function(){
                if( loc.latitude*1 <= 0 ){

                    app.com.getLocation(self,function(loc){
                        self.setData({
                            lat : loc.latitude,
                            lng : loc.longitude,
                        });
                        self.getData(id,zfid,loc.latitude,loc.longitude,false);
                    })
                }
            });
        });
        
    },
    getData(id,zfid,lat,lng,hidestate,fn){
        var self = this;
        var postdata = {
            id:id,
            op:'info',
            zfid:zfid,
            lat : lat,
            lng : lng,
        };
        app.com.http('jizi','POST',postdata,0,false,'','',function(res){
            if (res.data && !res.data.errno) {

                self.setData({
                    inited : true,
                    good : res.data.data.info,
                })

                setTimeout(function(){
                    self.setData({
                        Pagetheme : res.data.data.params.style,
                        params : res.data.data.params,
                        headlist : res.data.data.headlist ? res.data.data.headlist : [],
                        tuijian : res.data.data.tuijian ? res.data.data.tuijian : [],
                        helplist : res.data.data.helplist ? res.data.data.helplist : [],
                        helpdata : res.data.data.helpdata ? res.data.data.helpdata : {},
                        isjoined : res.data.data.isjoined ? res.data.data.isjoined : {},
                        userinfo : res.data.data.userinfo ? res.data.data.userinfo : {},
                        sharedata : res.data.data.sharedata,
                        isneedauth : res.data.data.userinfo.nickname ? false : true,
                    },function(){
                        self.endTime();

                        if( self.data.helpdata.font ){
                            self.setData({
                                getfont : self.data.helpdata.font,
                                showgeted : true,
                                showgetedstr : self.data.helpdata.str
                            })
                        }

                        if( fn ) fn();
                        
                        var octoparse = require('../resource/wxParse/html2json2.js');
                        var transData = octoparse.parse(self.data.good.content);                   
                        self.setData({
                            nodes : transData,
                        })
                        
                    });

                    if( self.data.good.music ) {
                        self.playbg();
                    }

                    wx.setNavigationBarTitle({
                      title:  res.data.data.info.title,
                    });
                },5)

            }else{
                app.com.alert(res.data.message);
            }
        });
    },
    endTime(){
        var self = this;
        if( self.data.timer ) return false;
        self.countTime();

        self.data.timer = setInterval(function(){
            self.countTime();
        },1000);

    },
    countTime(){
        var self = this;
        var date = new Date();
        var time = date.getTime();

        var endTime = 0;
        if( self.data.good.status == 0 ) {
            endTime = self.data.good.end + '000';
        }
        if( self.data.good.status == 2 ) {
            endTime = self.data.good.start + '000';
        }

        if(endTime != 0){
            var lag = (endTime*1 - time);
            if(lag > 0){
                var second = Math.floor(lag/1000%60);     
                var minite = Math.floor(lag/1000/60%60);
                var hour = Math.floor(lag/1000/60/60%24);
                var day = Math.floor(lag/1000/60/60/24);
            }else{
                var second = 0;
                var minite = 0;
                var hour = 0;
                var day = 0;
                if( self.data.good.status == 0 ){
                    self.setData({
                        'good.status' : 3,
                    });
                }else if( self.data.good.status == 2 ){
                    self.setData({
                        'good.status' : 0,
                    });
                }

            }
            self.setData({
                Countdown : {day:day,hour:hour,minute:minite,second:second}
            });
        }
    },
    onPullDownRefresh: function () {
        if( this.data.isdown ) return false;
        this.setData({
            isdown : true,
        });
        
        this.onLoad( this.data.options );
        this.setData({
            isdown : false,
        });

        // 关闭下拉刷新
        wx.stopPullDownRefresh();

    },
    onShareAppMessage : function(){
        var self = this;
        return {
            title: self.data.sharedata.title,
            path: self.data.sharedata.path,
            imageUrl : self.data.sharedata.img,
        }
    },
    onShareTimeline(){
        return {
            title: this.data.sharedata.title,
            query: this.data.sharedata.query,
            imageUrl : this.data.sharedata.img,
        }
    },
    joinact(){

        if( this.data.isneedauth ){
            this.setData({
                showuserbtn : true,
            })
            return false;
        }

        this.setData({
            showjoin : true,
        });
        this.subMess();
    },
    joinactpost: function() {
        var self = this;
        
        var postdata = {
            gid:self.data.good.id,
            op:'join',
            name : self.data.userinfo.name,
            tel : self.data.userinfo.tel,
        };
        if( !postdata.name || postdata.name == '' ){
            app.com.toast('请填写姓名');return false;
        }
        if( !postdata.tel || postdata.tel == '' ){
            app.com.toast('请填写联系电话');return false;
        }
        if( !app.com.verify('mobile',postdata.tel) ){
            app.com.toast('请填写正确的联系电话');return false;
        }

        app.com.http('jizi','POST',postdata,0,true,'','',function(res){
            if (!res.data.errno && res.data) {
                if( res.data.data && res.data.data.font ){
                    self.setData({
                        showgeted : true,
                        showgetedstr : false,
                        getfont : res.data.data.font,
                    })
                    var font = self.data.good.font;
                    for (var i = 0; i < font.length; i++) {
                        if( font[i].font == res.data.data.font ){
                            self.setData({
                                ['good.font['+i+'].isact'] : 1,
                            })
                        }
                    }
                }

                self.setData({
                    showjoin : false,
                })

                self.onPullDownRefresh();
            }else{
                app.com.toast(res.data.message);
            }
        })
        self.subMess();
    },
    cposter(){
        var self = this;
        app.com.http('jizi','POST',{op:'getposter',id:self.data.good.id,path:self.data.sharedata.path},0,true,'','',function(res){
            if (!res.data.errno && res.data ) {
                self.setData({
                    posterurl : res.data.data.url
                });
                app.com.alert('可长按图片保存到本地相册和发给好友！',function(){
                    wx.previewImage({
                        urls : [res.data.data.url]
                    })
                });
            }else{
                app.com.toast(res.data.message);
            }
        });
    },
    showjoin(){
        this.setData({
            showjoin : !this.data.showjoin,
        });
    },
    hidegeted : function(){
        this.setData({
            showgeted : false,
        });
    },
    name : function(e){
        this.setData({
            'userinfo.name' : e.detail.value,
        });
    },
    tel : function(e){
        this.setData({
            'userinfo.tel' : e.detail.value,
        });
    },
    location : function( e ){
        app.com.location( e );
    },
    otherapp : function( e ){
        app.com.otherapp( e );
    },
    navigateto : function( e ){
        app.com.navigateto( e );
    },
    redirectto : function( e ){
        app.com.redirectto( e );
    },    
    callphone : function(e){
        app.com.callphone( e );
    },
    toweburl : function(e){
        app.com.toweburl( e );
    },
    showimages : function( e ){
        app.com.showimages( e );
    },
    switchTab(e){
        if( this.data.bgmusic ){
            this.data.bgmusic.destroy();
        }
        wx.switchTab({
            url: e.currentTarget.dataset.url
        });
    },
    footNavigate(e){
        var url = e.currentTarget.dataset.url;
        wx.switchTab({
          url: url
        })
    },
    FormIdsubmit : function(e){
        if( app.globalData.formarr.length <= 20 ) app.globalData.formarr.push(e.detail.formId);
    },
    updateUserInfo : function(e){
        var self = this;
        
        app.com.getUserInfo(function(res){
            self.setData({
                showuserbtn : false,
                isneedauth : false,
            });
            self.joinact();
        },false,e.detail);
    },
    hideauth(){
        this.setData({
            showuserbtn:false,
        });
    },
    playbg(){
        var self = this;
        if( self.data.bgmusic ){
            self.data.bgmusic.destroy();
        }
        self.data.bgmusic = wx.createInnerAudioContext();
        self.data.bgmusic.autoplay = true;
        self.data.bgmusic.loop = true;
        self.data.bgmusic.src = self.data.good.music;
        self.data.bgmusic.play();
        self.setData({
            audioplaying : true
        });
    },
    replaybg(type){
        var self = this;
        if( type == 1 ){
            self.setData({
                audioplaying : true,
            });
            self.data.bgmusic.play();
        }else if(type == 2){
            self.setData({
                audioplaying : false,
            });
            self.data.bgmusic.pause();
        }else{
            self.setData({
                audioplaying : !self.data.audioplaying,
                closebg : !self.data.closebg,
            });
            if( self.data.audioplaying ){
                self.data.bgmusic.play();
            }else{
                self.data.bgmusic.pause();
            }
        }
    },
    subMess(){
        var self = this;
        wx.requestSubscribeMessage({
            tmplIds : self.data.params.submess,
            complete : function(){}
        })        
    }
}))